/*
 * Copyright (C) 2009 Android Shuffle Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dodgybits.shuffle.android.editor.activity;


import java.util.ArrayList;

import org.dodgybits.android.shuffle.R;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import roboguice.activity.RoboActivity;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

/**
 * A generic activity for editing a task in the database.  This can be used
 * either to simply view a task (Intent.VIEW_ACTION), view and edit a task
 * (Intent.EDIT_ACTION), or create a new task (Intent.INSERT_ACTION).  
 */
public class TaskEditorMapActivity extends MapActivity implements OnTouchListener, OnClickListener
{
	private MapView map = null;
	private MapOverlay overlay = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.task_editor_map);
		this.map = (MapView) findViewById(R.id.mapview);
		this.map.setBuiltInZoomControls(true);
		this.map.displayZoomControls(false);
		this.map.setOnClickListener(this);
		this.map.setOnTouchListener(this);
		this.overlay = new MapOverlay();
//		GeoPoint point = this.map.getMapCenter();
		GeoPoint point = new GeoPoint(19240000,-99120000);
		OverlayItem item = new OverlayItem(point, "Test", "Test description");
		this.overlay.addItem(item);
		this.map.getOverlays().add(this.overlay);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		//float x = event.getX();
		//float y = event.getY();
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		//v.get
	}
	
	private class MapOverlay extends ItemizedOverlay<OverlayItem> {

		private ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
		
		public MapOverlay() {
			super(boundCenter(getResources().getDrawable(R.drawable.shuffle_icon)));
		}
		
		@Override
		protected boolean onTap(int index) {
			OverlayItem item = this.items.get(index);
			AlertDialog.Builder dialog = new AlertDialog.Builder(TaskEditorMapActivity.this);
			dialog.setTitle(item.getTitle());
			dialog.setMessage(item.getSnippet());
			dialog.show();
			return true;
		}
		
		@Override
		public boolean onTouchEvent(MotionEvent event, MapView mapView) {
			// TODO Auto-generated method stub
			if(event.getAction() == MotionEvent.ACTION_MOVE) {
				AlertDialog.Builder dialog = new AlertDialog.Builder(TaskEditorMapActivity.this);
				dialog.setTitle("MoveDetected");
				int i = event.getHistorySize();
				event.getHistoricalX(i-1);
				String message = "bX=" + event.getHistoricalX(0) + " bY=" + event.getHistoricalY(0);
				message += " eX=" + event.getHistoricalX(i-1) + " eY=" + event.getHistoricalY(i-1);
				dialog.setMessage(message);
				dialog.show();
			}
			return super.onTouchEvent(event, mapView);
		}

		public void addItem(OverlayItem item) {
			Log.i("MyMap", "AddCalled");
			this.items.add(item);
			populate();
		}

		@Override
		protected OverlayItem createItem(int i) {
			Log.i("MyMap", "CreateCalled");
			return this.items.get(i);
		}

		@Override
		public int size() {
			return this.items.size();
		}
		
	}
        
}
